package com.ybkj.daijia.api.weixin.handler.impl;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.WxSetting;
import com.ybkj.daijia.api.weixin.WxMsgType;
import com.ybkj.daijia.api.weixin.entity.WxBaseMsgEntity;
import com.ybkj.daijia.api.weixin.entity.WxBaseRespEntity;
import com.ybkj.daijia.api.weixin.entity.WxItemPicDescEntity;
import com.ybkj.daijia.api.weixin.entity.WxMsgEventEntity;
import com.ybkj.daijia.api.weixin.entity.WxRespPicDescEntity;
import com.ybkj.daijia.api.weixin.entity.WxRespTextEntity;
import com.ybkj.daijia.server.model.WxBind;
import com.ybkj.daijia.server.service.WxBindService;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxQueryMyOrderHandler extends AbstractWxEventHandler {

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private WxBindService wxBindService;


    @Override
    public boolean canHandle(WxBaseMsgEntity msg) {
        if (super.canHandle(msg)) {
            if ("myorder".equals(((WxMsgEventEntity) msg).getEventKey())) {
                return true;
            }
        }

        return false;
    }


    @Override
    public WxBaseRespEntity handleEvent(WxMsgEventEntity msg,
        Map<String, Object> context, WxBaseRespEntity resp) {

        WxBind bind = wxBindService.findByWxuser(msg.getFromUserName());

        if (null == bind || bind.getStatus() == WxBind.WXBIND_STATUS_WAIT) {

            WxSetting setting = settingUtils.getWx();

            WxRespTextEntity text = new WxRespTextEntity();
            text.setContent(setting.getWxNoBindAccount());//没有绑定账号提醒
            text.setCreatedDate(new Date());
            text.setCreateTime(text.getCreatedDate().getTime());
            text.setFromUserName(msg.getToUserName());
            text.setToUserName(msg.getFromUserName());
            text.setFuncFlag(0);
            text.setMsgType(WxMsgType.text.name());

            return text;
        } else {

            WxSetting setting = settingUtils.getWx();

            WxRespPicDescEntity news = new WxRespPicDescEntity();

            List<WxItemPicDescEntity> articles = new LinkedList<WxItemPicDescEntity>();

            WxItemPicDescEntity item = new WxItemPicDescEntity();
            item.setTitle("我的订单");
            String pic = String.format("%sassets/img/wx_myorder.png", setting.getWxServer());
            String url = String.format("%s/wx/v1/aboutus", setting.getWxServer());
            item.setPicUrl(pic);
            item.setUrl(url);
            articles.add(item);

            item = new WxItemPicDescEntity();

            pic = String.format("%sassets/img/current_order.png", setting.getWxServer());
            url = String
                .format("%swx/v1/todayOrder?phone=%s", setting.getWxServer(), bind.getPhone());

            item.setTitle("当前订单");
            item.setPicUrl(pic);
            item.setUrl(url);
            articles.add(item);

            item = new WxItemPicDescEntity();

            pic = String.format("%sassets/img/history_order.png", setting.getWxServer());
            url = String.format("%swx/v1/historyOrder?phone=%s&pager.offset=0&limit=10",
                setting.getWxServer(), bind.getPhone());

            item.setTitle("历史订单");
            item.setPicUrl(pic);
            item.setUrl(url);
            articles.add(item);

            news.setArticles(articles);
            news.setCreatedDate(new Date());
            news.setCreateTime(news.getCreatedDate().getTime());
            news.setFromUserName(msg.getToUserName());
            news.setToUserName(msg.getFromUserName());
            news.setFuncFlag(0);
            news.setMsgType(WxMsgType.news.name());

            return news;
        }
    }

}
